/**
 * 
 */
package test1;

import java.util.ArrayList;
import java.util.List;

import lib.combination.Combination;
import lib.combination.CombinationImpl;
import lib.combination.Permutation;
import lib.combination.PermutationImpl;

import org.apache.commons.lang.StringUtils;
import org.junit.Test;

/**
 * @author <a href="www.sureinterview.com">SureInterview</a>
 * 
 */
public class Matrix1 {

	void generate(int n, int m) {
		List<Integer> segEnd = new ArrayList<Integer>();
		for (int i = 0; i < n - 1; i++) {
			segEnd.add(i);
		}
		Combination<Integer> combn = new CombinationImpl<Integer>(segEnd, m - 1);

		for (List<Integer> lastPos : combn) {
			Integer[] codeSeed = new Integer[n];
			lastPos.add(0, -1);
			lastPos.add(n - 1);

			// lastPos : ascending position number. In the case of f(4,3), the
			// first lastPos is {-1,0,1,3}
			for (int i = 0; i < m; i++) {
				// current segment is in range ( loasPos[i]+1, lastPos[i+1] )
				for (int j = lastPos.get(i) + 1; j <= lastPos.get(i + 1); j++) {
					codeSeed[j] = i;
				}
			}

			// codeSeed : the ascending coding. In the case of f(4,3), the first
			// codeSeed is {0,1,2,2}
			Permutation<Integer> perm = new PermutationImpl<Integer>(codeSeed);

			// get all matrixes based on the seed code.
			for (Integer[] code : perm) {
				System.out.println(StringUtils.join(code, ","));
			}
		}
	}

	@Test
	public void test() {
		generate(4, 3);
	}
}
